<?php

namespace app\common\validate;

use app\common\validate\Base;

class Member extends Base
{

    protected $rule = [
        'id|用户ID' => 'require|integer',
        'register_name|用户编号' => 'require|checkRegisterName',
        'mid|编号' => 'require|unique:member|max:20',
        'type|类型' => 'require|in:1,2',
        'area|区域' => 'require|in:1,2',
        'level_id|等级' => 'require|integer',
        'inviter_id|推荐人' => 'require|checkInviter',
        'seter_id|安置人' => 'require|checkSeter',
        'agent_id|代理中心' => 'require|checkAgent',
        'password|登录密码' => 'require|length:6,16',
        'rpassword|重复密码' => 'require|confirm:password',
        'safe_password|安全密码' => 'require',
        'rsafe_password|重复安全密码' => 'require|confirm:safe_password',
        'mname|会员真实姓名' => 'require|length:2,20',
        'phone|手机号码' => 'requrie|length:11',
        'smscode|短信验证码' => 'require|length:4|checkSmsCode'
    ];

    protected $message = [
        'rpassword.eq' => '重复登录密码输入错误',
        'rsafe_password.eq' => '重复安全密码密码输入错误',
    ];

    protected $scene = [
        // 后台新增会员
        'manage_add' => ['mid','type','area','level_id','inviter_id','seter_id','password','rpassword','safe_password','rsafe_password','mname','agent_id'],
        // 后台编辑会员
        'manage_edit' => ['id','mname'],
        // 报单中心新增会员
        'agent_add' =>  ['mid','type','area','level_id','inviter_id','seter_id','password','rpassword','safe_password','rsafe_password','mname'],
        // 前台修改密码
        'home_pwd' => ['password','rpassword','smscode'],
        // 前台修改交易密码
        'home_safepwd' => ['safe_password','rsafe_password','smscode'],
        'edit' => []
    ];

    /**
     * 验证注册用户编号
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkRegisterName($v,$rule,$data){
        $memberModel = model('member');
        $user = $memberModel->where(['mid'=>$v])->whereOr(['phone'=>$v])->find();
        if($user){
            $this->message['register_name.checkRegisterName'] = '用户编号已经存在';
            return false;
        }else{
            return true;
        }
    }

    protected function checkInviter($v, $rule, $data)
    {
        $memberModel = new \app\common\model\Member;
        $memberSeterModel = new \app\common\model\MemberSeter;

        $inviter = $memberModel->where(['mid'=>$v])->find();
        if(!$inviter){
            $this->message['inviter_id.checkInviter'] = '推荐人不存在';
            return false;
        }

        $seter_mid = strtoupper($data['seter_id']);
        $seter = $memberModel->where(['mid'=>$seter_mid])->find();
        $seter_id = $seter['id'];
        // 查询安置人的安置路径
        $seter_seters = $memberSeterModel->getSetersByUid($seter_id);
        if(!$seter_seters){
            $seter_seters = [];
        }else{
            $seter_seters = array_column($seter_seters,'seter_uid');
        }
        // 加上安置人本身
        $seter_seters[] = $seter_id;
        // 推荐人uid是否存在与安置路径中
        if(!in_array($inviter['id'],$seter_seters)){
            $this->message['inviter_id.checkInviter'] = '推荐人不在安置网络内';
            return false;
        }

        return true;
    }

    protected function checkSeter($v, $rule, $data)
    {
        $memberModel = new \app\common\model\Member;
        $seter = $memberModel->where(['mid'=>$v])->find();
        if(!$seter) {
            $this->message['seter_id.checkSeter'] = '安置人不存在';
            return false;
        }
        $member = $memberModel->where(['seter_id'=>$seter['id'],'area'=>$data['area']])->find();
        if($member) {
            $this->message['seter_id.checkSeter'] = '该区域已被安置';
            return false;
        }
        return true;
    }

    protected function checkAgent($v)
    {
        $memberModel = new \app\common\model\Member;
        $agent = $memberModel->where(['mid'=>$v])->find();
        if(!$agent) {
            $this->message['agent_id.checkAgent'] = '代理中心 '.$v.' 不存在';
            return false;
        }
        if(1 != $agent['is_agent']) {
            $this->message['agent_id.checkAgent'] = '会员 '.$v.' 不是报单中心';
            return false;
        }
        return true;
    }
}